//
//  ViewController.swift
//  L14_05_UIViewInsert
//
//  Created by yao_yu on 14-10-8.
//  Copyright (c) 2014年 yao_yu. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        setupViews()
    }
    
    func setupViews(){
        let parent = UILabel(frame: CGRectMake(0, 20, view.bounds.width, view.bounds.height-20))
        parent.textAlignment = NSTextAlignment.Center
        parent.text = "父标签"
        view.addSubview(parent)
        
        //追加子标签
        let width = parent.bounds.width
        for index_ in 0...3{
            let index = CGFloat(index_)
            let child = UILabel(frame: CGRectMake(0, (index * 30), width, 40))
            child.text = "子标签 \(index_)"
            child.textAlignment = NSTextAlignment.Center
            child.backgroundColor = UIColor(red: (0.25 + index/4), green: (1.0 - index/4.0), blue: (0.25 + index/4), alpha: 1)
            child.tag = index_ + 1
            parent.insertSubview(child, atIndex: 0)
        }
        
        //标签前方显示
        var stext =  (parent.subviews[2] as UILabel).text!
        parent.bringSubviewToFront(parent.subviews[2] as UIView)
        
        //标签放到后面
        stext +=  (parent.subviews[2] as UILabel).text!
        parent.sendSubviewToBack(parent.subviews[2] as UIView)
        
        //在使用标签位置设置后, subviews中子视图的排列顺序会发生改变
        stext +=  (view.viewWithTag(1)! as UILabel).text!
        parent.bringSubviewToFront(view.viewWithTag(1)!)
        parent.text = stext

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

